package com.cjl.auth.utils;

import com.cjl.model.system.SysDept;
import com.cjl.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class DeptHelper {
    public static List<SysDept> buildTree(List<SysDept> sysDeptList) {
        List<SysDept> result = new ArrayList<>();
        for (SysDept sysDept : sysDeptList) {
            if(sysDept.getParentId() == 0){
                result.add(getChildren(sysDept, sysDeptList));
            }
        }
        return result;
    }

    private static SysDept getChildren(SysDept sysDept, List<SysDept> sysDeptList){
        sysDept.setChildren(new ArrayList<>());
        for (SysDept item : sysDeptList) {
            if(item.getParentId().longValue() == sysDept.getId().longValue()){
                if(item.getParentId() == null){
                    sysDept.setChildren(new ArrayList<SysDept>());
                }
                sysDept.getChildren().add(getChildren(item, sysDeptList));
            }
        }
        return sysDept;
    }
}
